<template>
  <div class="addResource">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="45%"
      :before-close="closeDialog">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="资源名称">
          <el-input v-model="form.name" size="small"></el-input>
        </el-form-item>
        <el-form-item label="资源路径">
          <el-input v-model="form.url" size="small"></el-input>
        </el-form-item>
        <el-form-item label="资源分类">
          <el-select v-model="form.categoryId" size="small">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" size="small">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="doSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getResourceSave, getResourceCategory } from '@/services/resource'
export default {
  name: 'addResource',
  data () {
    return {
      // 设置 dialog 对话框的开启或关闭
      dialogVisible: false,
      form: {
      },
      categoryList: [],
      title: '添加资源'
    }
  },
  created () {
    this.getResourceCategory()
  },
  methods: {
    // 关闭弹窗
    closeDialog () {
      this.dialogVisible = false
      this.form = {}
    },
    // 开启弹窗
    onpenDialog () {
      this.dialogVisible = true
    },
    // 获取当前的资源信息
    async getResourceCategory () {
      const { data } = await getResourceCategory()
      if (data.code === '000000') {
        this.categoryList = data.data
      }
    },
    // 添加资源
    async doSave () {
      const { data } = await getResourceSave(this.form)
      if (data.code === '000000') {
        this.$message.success('提交成功')
      } else {
        this.$message.error('数据提交失败')
      }
      // 关闭dialog弹窗
      this.closeDialog()
      // 触发监听事件,使得父组件更新数据
      this.$emit('refreshData')
    },
    // 获取某项资源信息
    // 通过父组件传递过来的数据,更新编辑页信息
    getResourceItem (val) {
      // 打开弹窗
      this.onpenDialog()
      this.form = val
      this.title = '编辑信息'
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
